package com.ytjj.qmyx.admin.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.gexin.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.admin.config.SupplyApiConfig;
import com.ytjj.qmyx.admin.dao.BankerBrandDao;
import com.ytjj.qmyx.admin.mapper.AdminMapper;
import com.ytjj.qmyx.admin.model.request.BankerBrandRequest;
import com.ytjj.qmyx.admin.model.response.BankerBrandBasicInfoResponse;
import com.ytjj.qmyx.admin.model.vo.BankerBrandVO;
import com.ytjj.qmyx.admin.service.BankerBrandService;
import com.ytjj.qmyx.mall.mapper.ProductCategoryMapper;
import com.ytjj.qmyx.mall.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BankerBrandServiceImpl implements BankerBrandService {

    @Autowired
    private BankerBrandDao bankerBrandDao;
    @Autowired
    private BankerInfoServiceImpl bankerInfoService;
    @Resource
    ProductCategoryMapper productCategoryMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Resource
    private SupplyApiConfig supplyApiConfig;

    @Override
    public CommonPage<BankerBrandVO> selectList(BankerBrandRequest bankerBrandRequest) {
        PageHelper.startPage(bankerBrandRequest.getPageNum(), bankerBrandRequest.getPageSize());
//        String curUserEmail = UserUtil.getPrincipal();
//        int adminId =adminMapper.adminId(curUserEmail);
//        bankerBrandRequest.setAdminId(adminId);
        List<BankerBrandVO> pageList = bankerBrandDao.selectList(bankerBrandRequest);
        for (int i = 0; i<pageList.size();i++) {
            BankerBrandVO cateId = pageList.get(i);
            if (StringUtils.isEmpty(cateId.getCategoryId())) {
                continue;
            }
            List<String> paramList = Arrays.asList(cateId.getCategoryId().split(","));
            List<Integer> categoryIds = paramList.stream().map(Integer::parseInt).collect(Collectors.toList());
            ProductCategoryExample productCategoryExample = new ProductCategoryExample();
            productCategoryExample.createCriteria().andIdIn(categoryIds);
            List<ProductCategory> list =  productCategoryMapper.selectByExample(productCategoryExample);
            String titles = list.stream().map(p->p.getTitle()).collect(Collectors.joining(","));

            cateId.setCategoryTitles(titles);
        }
        pageList.stream().forEach(obj -> {
            if (null == obj.getBrandType()) {
                obj.setBrandType(obj.getBankerType());
            }
            List<BankerBrandImg> imgList = bankerBrandDao.selectAllImgList(obj.getId());
            if (CollectionUtils.isEmpty(imgList)) {
                imgList = new ArrayList<>();
                BankerInfo bankerInfo = bankerInfoService.getBankerInfoStatus(obj.getBankerId());

                List<String> list1 = new ArrayList<>();
                if (!StringUtils.isEmpty(bankerInfo.getRegisterCertificate())) {
                    list1 = Arrays.asList(bankerInfo.getRegisterCertificate().split(","));
                }
                for (String imgUrl : list1) {
                    BankerBrandImg type1Img = new BankerBrandImg();
                    type1Img.setType(1);
                    type1Img.setImgUrl(imgUrl);
                    imgList.add(type1Img);
                }

                List<String> list2 = new ArrayList<>();
                if (!StringUtils.isEmpty(bankerInfo.getBrandLicensing())) {
                    list2 = Arrays.asList(bankerInfo.getBrandLicensing().split(","));
                }
                for (String imgUrl : list2) {
                    BankerBrandImg type2Img = new BankerBrandImg();
                    type2Img.setType(2);
                    type2Img.setImgUrl(imgUrl);
                    imgList.add(type2Img);
                }
            }
            obj.setBankerBrandImgs(imgList);
        });

        return CommonPage.restPage(pageList);
    }

    @Override
    public String newSelectList(BankerBrandRequest bankerBrandRequest) {
        String json = JSON.toJSONString(bankerBrandRequest);
        String resultJson = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/banker/brandList", json, supplyApiConfig.getHeadsMap());
        return resultJson;
    }

    @Override
    public CommonPage<ProductCategory> dropList() {
        List<ProductCategory> List = bankerBrandDao.dropList();
        return CommonPage.restPage(List);
    }

    @Override
    public CommonResult queryAllBrandListByBankerId(Integer bankerId) {
        List<BankerBrandBasicInfoResponse> brandList = bankerBrandDao.selectByBankerIdAndStatus(bankerId, 1);
        return CommonResult.success(brandList);
    }

    @Override
    public BankerBrand selectById(Integer id) {
        return bankerBrandDao.selectById(id);
    }
}
